Imports BikiSoft.Comm

Public Class TraceFind

    Dim m_Downloads As New ArrayList


    Private Sub cmdDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDownload.Click
        Dim f_data(&H3FFF) As Byte
        Dim f_start As Long = &H1000
        Dim f_end As Long = &H11FF
        Dim f_ret As BK_return
        Dim f_table As Table
        'reDo:   f_ret = DownloadConsult(f_data, f_start, f_end)
        '        If f_ret <> BK_return.BK_OK Then
        '            Err_message(Err_Decode(f_ret))
        '            If MsgBox("Retry?", MsgBoxStyle.RetryCancel, "Resume Download") = MsgBoxResult.Retry Then GoTo reDo
        '        End If

        While cbGo.Checked = True
reDo2:      f_start = vsMin.Value
            'f_end = &H380F
            f_end = vsMin.Value + &H10
            f_table = New Table(f_start, 16, 1, "stuff")

            f_ret = DownloadConsult(f_data, f_start, f_end)
            If f_ret <> BK_return.BK_OK Then
                Err_message(Err_Decode(f_ret))
                If MsgBox("Retry?", MsgBoxStyle.RetryCancel, "Resume Download") = MsgBoxResult.Retry Then GoTo reDo2
                Exit While

            End If

            gridPrev.populate(f_table, f_data)
            'm_Downloads.Add(f_data)
        End While
    End Sub

    Private Function DownloadConsult(ByRef oData As Byte(), ByRef iStart As Long, ByRef iEnd As Long) As BK_return
        Dim f_frame As Comm_Frame = Nothing
        Dim f_packet As Byte() = Nothing
        Dim f_return As Byte() = Nothing
        Dim f_cmd() As Byte = cv_BytesFromHex("30C90000C90000C90000C90000F0")
        'Dim f_data(&HFFFF) As Byte
        Dim f_retry As Integer = 0
        Dim i As Long
        Dim f_index As Integer = 0
        Dim f_returnCode As BK_return = BK_return.BK_OK

resend: If BikiRom_Init() = BK_return.BK_OK Then
            Try

                m_BikiInfo.TimeOut = 2000
                ' get into consult bypass mode
                f_frame = New Comm_Frame(Comm_Frame.BikiFrameType.ID_CONSULT_PASS, , Comm_Frame.BikiFrameAction.CMD_GO, , Comm_Frame.BikiFrameType.ID_LAST_COMMAND_STATUS)
                f_frame.Send()

                ' send the initialisation
                f_frame = New Comm_Frame(Comm_Frame.BikiFrameType.ID_ECU, , , New Byte() {1, &HFF, &HFF, &HEF}, Comm_Frame.BikiFrameType.ID_CONSULT_PASS)
                f_frame.Send(f_return, True)

                For i = iStart To iEnd Step 4
                    Application.DoEvents()

                    ' Build the command
                    For s As Integer = 0 To 3
                        f_cmd((s * 3) + 2) = HIBYTE(s + i)
                        f_cmd((s * 3) + 3) = LOBYTE(s + i)
                    Next

                    f_frame = New Comm_Frame(Comm_Frame.BikiFrameType.ID_ECU, , , f_cmd, Comm_Frame.BikiFrameType.ID_CONSULT_PASS)
                    If f_frame.Send(f_return, True, 2) = Comm_Frame.BikiFrameResponse.CMD_OK AndAlso _
                       Not IsNothing(f_return) AndAlso f_return.Length >= &H30 Then

                        Array.Copy(f_return, 2, oData, i, 4)

                        For s As Integer = 0 To 10
                            If f_return(s) = 54 And _
                                f_return(s + 1) = f_cmd(3) And _
                                f_return(s + 2) = f_cmd(4) And _
                                f_return(s + 3) = 54 And _
                                f_return(s + 4) = f_cmd(6) And _
                                f_return(s + 5) = f_cmd(7) And _
                                f_return(s + 6) = 54 And _
                                f_return(s + 7) = f_cmd(9) And _
                                f_return(s + 8) = f_cmd(10) And _
                                f_return(s + 9) = 54 And _
                                f_return(s + 10) = f_cmd(12) And _
                                f_return(s + 11) = f_cmd(13) And _
                                f_return(s + 12) = &HFF And _
                                f_return(s + 13) = &H4 _
                            Then
                                f_index = s
                                Exit For
                            End If
                        Next

                        If f_index <> -1 Then
                            Array.Copy(f_return, 14 + f_index, oData, i, 4)
                            If i Mod 16 = 0 Then Console.WriteLine()
                            Console.Write(cv_HexFromBytes(New Byte() {oData(i), oData(i + 1), oData(i + 2), oData(i + 3)}))
                            f_retry = 0
                        Else
                            If pp(f_retry) < 10 Then
                                i -= 4
                            Else
                                f_returnCode = BK_return.BK_IO_ERROR_BIK
                                Exit Try
                            End If
                        End If


                        f_frame = New Comm_Frame(Comm_Frame.BikiFrameType.ID_ECU, , , New Byte() {1, &H30}, Comm_Frame.BikiFrameType.ID_CONSULT_PASS)
                        f_frame.Send(f_return, True)

                        Threading.Thread.Sleep(10)
                    Else
                        If pp(f_retry) < 10 Then
                            i -= 4
                        Else
                            f_returnCode = BK_return.BK_IO_ERROR_BIK
                            Exit Try
                        End If
                    End If

                    If i >= pbDown.Minimum And i <= pbDown.Maximum Then pbDown.Value = i
                    pbDown.Caption = "0x" & Hex(i)
                Next

                f_returnCode = BK_return.BK_OK
            Catch exComm As Comm_Exceptions
                If pp(f_retry) < 10 Then iStart = i : close_bikirom() : GoTo resend
                f_returnCode = BK_return.BK_IO_ERROR_BIK
            Catch exUsb As USB_exceptions
                If pp(f_retry) < 10 Then iStart = i : close_bikirom() : GoTo resend
                f_returnCode = BK_return.BK_IO_ERROR_USB
            End Try

            'm_BikiInfo.reset()
            'close_bikirom()
            iStart = i
            Return f_returnCode
        Else
            Return BK_return.BK_FAILED_INIT
        End If
    End Function

    Private Sub cmdCompare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCompare.Click
        Dim f_table As New Table(4096, 16, 768, "Table")
        Dim f_data() As Byte = Nothing

        If m_Downloads.Count > 1 Then
            f_data = CType(m_Downloads.Item(m_Downloads.Count - 1), Byte())
        ElseIf m_Downloads.Count > 0 Then
            f_data = CType(m_Downloads.Item(0), Byte())
        End If

        gridMain.populate(f_table, f_data)
    End Sub

    Private Sub gridMain_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridMain.CellEnter, gridMain.CellClick
        If e.RowIndex >= 0 And e.ColumnIndex >= 0 Then
            Dim f_cell As Controls.libGridCell = CType(gridMain.Item(e.ColumnIndex, e.RowIndex), Controls.libGridCell)
            If m_Downloads.Count > 0 And Not IsNothing(f_cell) Then
                Dim f_data(m_Downloads.Count - 1) As Byte
                For i As Integer = 0 To m_Downloads.Count - 1
                    f_data(i) = CType(m_Downloads.Item(i), Byte()).GetValue(f_cell.Address)
                Next
                Dim f_table As New Table(0, m_Downloads.Count, 1, "stuff")
                gridPrev.populate(f_table, f_data)
            End If
        End If
    End Sub

    Private Sub vsMin_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles vsMin.ValueChanged
        Me.lbAddr.Text = "0x" & Hex(vsMin.Value)
    End Sub
End Class